<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Contains the Basic Information section for the show page.
 *  This should not be triggered independently, but as a component of another page.
 */


/**
 *  Display the basic information page
 *  @param $a_allowEdit If true, we display an edit version of the component
 *  @param $showID      ID of the show to display
 *  @param $model       The show model
 *  @param $showData    data pertaining this show
 */
function displayBasicInformation( $a_alowEdit = false, $showID, &$model, &$showData )
{
    $phpdate  = strtotime($showData['datetime']);
    $dateInfo = date_parse($showData['datetime']);
    $hour     = (int)$dateInfo['hour'];
    $minute   = (int)$dateInfo['minute'];
    $pm       = false;
        
    if($hour > 12){
        $hour -=12;
        $pm   = true;
    }    
    
    ?>
    <script language="javascript" type="text/javascript">
    <!--
    
        // Makes sure the provided date is on the yyyy-mm-dd format
        function isValidDate( theDate )
        {
            // 1. Check for format
            var dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;
            if( !dateFormat.test(theDate) )
            {
                return false;
            }
            
            // 2. Check for ranges
            var components = theDate.split('-');
            var dateObj    = new Date( components[0], components[1] - 1, components[2] );
                
            if( (dateObj.getFullYear() != components[0]) || (dateObj.getMonth() + 1 != components[1]) || (dateObj.getDate() != components[2]) )
            {
                return false;
            }
            
            return true;
        }
        
        // Validate and submit show modifications
        _jq(function(){
            var baseForm = _jq('form[name="baseForm"]');
            baseForm.submit(function(){
                
                // date
                if(!isValidDate( _jq('#show-date').val() ))
                {
                    alert('Please indicate a valid show date');
                    return false;
                }
                
                // Configure date and time
                var hour = parseInt( _jq('select#show-baseHour option:selected').val() );
                var min  = parseInt( _jq('select#show-baseMinute option:selected').val() );
                var date = _jq('input#show-date').val();
                
                if( _jq('select#show-basePart option:selected').val() == 'pm' )
                {
                    hour += 12;
                }
                            
                var dateTime = date + ' ' + hour + ':' + min + ':00';
                _jq('[name="datetime"]').val( dateTime );
                
                // Submit
                _jq.ajax({
                    type: "POST",
                    url: "<?php echo JRoute::_('index.php?');?>",
                    data: baseForm.serialize(),
                    success: function(msg)
                    {
                        alert('Basic information updated');
                    }
                });
                
                return false;
                
            });        
        });
        
    
    -->
    </script>
    
    
    <section id='show-data'>
        <div id='section-title'>
            <h1>Basic Information</h1>
        </div>
        <form id='form-basic-information' name='baseForm' >
            <fieldset class='show-data-left'>
                <ol>
                    <li>
                        <label for="contact">Main contact</label>
                        <input type="text" id="show-contact" name="contact" maxlength="45" style="width:160px;" value="<?php echo $showData['contact']; ?>" />
                    </li>
                    <li>
                        <label for="date">Date (yyyy-mm-dd)</label>
                        <?php echo JHTML::_('calendar',date("Y-m-d",$phpdate),'date', 'show-date', '%Y-%m-%d'); ?>                    
                    </li>                        
                    <li>
                        <label for="time">Time</label>
                        <select id='show-baseHour'>
                            <?php
                            for($i = 1; $i<=12; $i++)
                            {
                                if ($i == $hour){
                                    echo '<option selected>'.$i.'</option>';
                                } else {
                                    echo '<option>'.$i.'</option>';
                                }
                            }
                            ?>
                        </select>
                        :
                        <select id='show-baseMinute' >>
                            <?php
                            for($i=0; $i<=60; $i+=10)
                            {
                                if($i == $minute){
                                    echo '<option selected>'.$i.'</option>';
                                } else {
                                    echo '<option>'.$i.'</option>';
                                }
                            }
                            ?>
                        </select>
                        /
                        <select id='show-basePart'>
                            <?php
                            if($pm){
                                echo '<option>am</option><option selected>pm</option>';
                            } else {
                                echo '<option selected>am</option><option>pm</option>';
                            }                                
                            ?>
                        </select>                    
                    </li>
                    <li>
                        <label for="cost">Cost</label>
                        <input type="text" id="show-pay" name="pay" value="<?php echo $showData['pay']; ?>" />
                    </li>
                    <li>
                        <label for="status">Current status</label>
                        <select class="show-basics-form-status" id="show-status" name="status">
                            <?php echo $model->getStatusHTMLCombo($showData['status']); ?>
                        </select>
                    </li>                    
                </ol>
            </fieldset>
            <fieldset class='show-data-right'>
                <label for="notes" style="width:100%; text-align:left;">Notes</label>
                <textarea name="notes" id="show-notes" style="width: 80%; height:80%;"><?php echo $showData['notes']; ?></textarea>
            </fieldset>
            <fieldset class='show-data-save'>
                <input type="submit" name="submit" value="Save changes"/>
                and notify 
                <select name='notify'>
                    <?php echo $model->getNotificationTargetsHTMLCombo(); ?>
                </select>                    
            </fieldset>
            <input type="hidden" name="option"     value="com_musashi" />
            <input type="hidden" name="task"       value="updateShow" />
            <input type="hidden" name="controller" value="shows" />
            <input type="hidden" name="sid"        value="<?php echo $showID; ?>" />
            <input type="hidden" name="datetime"   value="" />                            
        </form>
    </section>
<?php    
}

?>